<template>
  <div class="app-container">
    <el-row>
      <el-col :span="24">
        <el-button @click="dialogFormVisible=true">添加菜单</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%" class="no-r-b"
      :header-cell-style="{background:'#FAFAFA',color:'#666666'}">
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="菜单操作名称">
        <template slot-scope="scope">
          <span>{{ scope.row.navaction }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="菜单名称">
        <template slot-scope="scope">
          <span>{{ scope.row.navname }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="所属模块">
        <template slot-scope="scope">
          <span>{{ scope.row.modname }}</span>
        </template>
      </el-table-column>

      <!-- <el-table-column width="100px" label="Importance">
        <template slot-scope="scope">
          <svg-icon v-for="n in +scope.row.importance" :key="n" icon-class="star" class="meta-item__icon" />
        </template>
      </el-table-column> -->

      <el-table-column class-name="status-col" label="状态">
        <template slot-scope="{row}">
          <el-tag :type="row.deleted_at | statusFilter">
            {{ row.deleted_at?'已禁用':'正常' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建日期">
        <template slot-scope="scope">
          <span>{{ scope.row.created_at | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" icon="el-icon-edit" @click="editShow(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <!--列表数据添加/编辑开始  -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" center width="95%" class="user">
      <el-form ref="ruleForm" :model="formRow" label-width="100px">
        <el-form-item label="所属模块">
          <el-select v-model="formRow.moid" placeholder="请选择所属模块">
            <el-option
              v-for="item in modules"
              :key="item.modspace"
              :label="item.modname"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="菜单名称">
          <el-input v-model="formRow.navname" auto-complete="off" />
        </el-form-item>
        <el-form-item label="菜单操作名称">
          <el-input v-model="formRow.navaction" auto-complete="off" />
        </el-form-item>
        <!-- <el-form-item> -->
          <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
          <!-- <el-button @click="resetForm('ruleForm')">重置</el-button> -->
        <!-- </el-form-item> -->
      </el-form>
    </el-dialog>
    <!--列表数据添加/编辑结束  -->
  </div>
</template>

<script>
import { getnav, getmodule, addNav, editNav } from '@/api/user'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import { Message } from 'element-ui'

export default {
  name: 'NavList',
  components: { Pagination },
  filters: {
    statusFilter(status) {
      if (status) {
        status = 'deleted'
      } else {
        status = 'published'
      }
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: [],
      modules: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20
      },
      // 弹窗标题
      dialogTitle: '菜单添加',
      dialogFormVisible: false,
      formRow: {
        navname: '',
        navaction: '',
        moid: 1
      }// 表单数据
    }
  },
  created() {
    this.getList()
    this.getMods()
  },
  methods: {
    // 获取列表数据
    getList() {
      this.listLoading = true
      getnav(this.listQuery).then(response => {
        const { data } = response
        this.list = data.list
        this.total = data.total
        this.listLoading = false
      })
    },
    // 获取所有模块
    getMods() {
      getmodule({ page: 1, limit: 100 }).then(response => {
        const { data } = response
        this.modules = data.list
      })
    },
    // 表单提交方法
    submitForm(formName) {
      const isadd = this.dialogTitle !== '菜单编辑'
      if (isadd) { // 新增数据
        addNav(this.formRow).then(res => {
          Message({
            message: '保存成功',
            type: 'success',
            duration: 2 * 1000
          })
          this.dialogFormVisible = false
          this.getList()
        })
      } else { // 编辑数据
        editNav(this.formRow).then(res => {
          Message({
            message: '保存成功',
            type: 'success',
            duration: 2 * 1000
          })
          this.dialogFormVisible = false
          this.getList()
        })
      }
    },
    // 搜索日期变化操作方法
    dateChange(options) {
      console.log(options, 'dateChange')
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    // 编辑
    editShow(row) {
      this.formRow = row
      this.dialogTitle = '菜单编辑'
      this.dialogFormVisible = true
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
